﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using Persons.Classes;
using Persons.Interfaces;
using Persons.DS;

namespace Persons.Tests
{
    [TestFixture]
    public class UT_DS_MOCK
    {
        List<Persona> expected;
        List<Persona> actual;
        Persona temp;
        IDS ds;

        private void Init(int _type)
        {
            ds = new DS_MOCK();
            actual = new List<Persona>();
            expected = new List<Persona>();
            switch (_type)
            {
                case 0:
                    {
                        break;
                    }
                case 1:
                    {
                        InitOne();
                        ds.Create(actual[0]);
                        break;
                    }
                case 2:
                    {
                        InitMoreThanOne();
                        foreach (Persona p in actual)
                        {
                            ds.Create(p);
                        }
                        break;
                    }
            }
        }
        private void InitMoreThanOne()
        {
            temp = new Persona(1, "Ivan", "Ivanov", 4, null);
            actual.Add(temp);
            temp = new Persona(10, "Petr", "Petrov", 23, null);
            actual.Add(temp);
            temp = new Persona(5, "Sidor", "Sidoriv", 17, null);
            actual.Add(temp);
            temp = new Persona(2, "Chyvak", "Chyvakov", 45, null);
            actual.Add(temp);
            temp = new Persona(3, "Adam", "Lambert", 78, null);
            actual.Add(temp);
            temp = new Persona(4, "Jessie", "J", 29, null);
            actual.Add(temp);
            temp = new Persona(6, "Sonya", "Sonyavna", 55, null);
            actual.Add(temp);
            temp = new Persona(7, "Kate", "Lavric", 32, null);
            actual.Add(temp);
        }
        private void InitOne()
        {
            temp = new Persona(1, "Ivan", "Ivanov", 4, null);
            actual.Add(temp);
        }
        private bool Compare(List<Persona> compList1, List<Persona> compList2)
        {
            bool res = false;
            if (compList1.Count != compList2.Count) res = false;
            else
            {
                res = true;
                for (int i = 0; i < compList2.Count; i++)
                {
                    if (compList2[i].ID != compList1[i].ID ||
                        compList2[i].Fname != compList1[i].Fname ||
                        compList2[i].Lname != compList1[i].Lname ||
                        compList2[i].Age != compList1[i].Age)
                    {
                        res = false;
                        break;
                    }
                }
            }
            return res;
        }

        [Test]
        public void TestForCreateWhenFileIsEmpty()
        {
            Init(0);
            ds.Create(new Persona(12, "Gregory", "House", 53,null));
            actual.Add(new Persona(12, "Gregory", "House", 53, null));
            expected = ds.Read();
            Assert.AreEqual(true, Compare(actual, expected));
        }
        [Test]
        public void TestForCreateWhenFileHasOneItem()
        {
            Init(1);
            ds.Create(new Persona(12, "Gregory", "House", 53, null));
            actual.Add(new Persona(12, "Gregory", "House", 53, null));
            expected = ds.Read();
            Assert.AreEqual(true, Compare(actual, expected));
        }
        [Test]
        public void TestForCreateWhenFileHasMoreThanOneItem()
        {
            Init(2);
            ds.Create(new Persona(12, "Gregory", "House", 53, null));
            actual.Add(new Persona(12, "Gregory", "House", 53, null));
            expected = ds.Read();
            Assert.AreEqual(true, Compare(actual, expected));
        }
        [Test]
        public void TestForReadID()
        {
            Init(2);
            expected = ds.Read(4);
            actual = new List<Persona>();
            actual.Add(new Persona(4, "Jessie", "J", 29, null));
            Assert.AreEqual(true, Compare(actual, expected));
        }
        [Test]
        public void TestForDeleteID()
        {
            Init(2);
            ds.Delete(4);
            expected = ds.Read();
            actual.RemoveAt(5);
            Assert.AreEqual(true, Compare(actual, expected));
        }
        [Test]
        public void TestForUpdatePersona()
        {
            Init(2);
            ds.Update(new Persona(4, "Jessie", "J", 19,null));
            expected = ds.Read();
            actual[5].Age = 19;
            Assert.AreEqual(true, Compare(actual, expected));
        }
    }
}
